import { queryConfigBykey, saveConfig } from "@/api/config";
import { uploadFile } from "@/api/file";
import { ADMIN_INFO_KEY } from "@/constants/configKey.constant";
import { ConfigType } from "@/types/config";
import { deleteFileInf } from "@/utils/deleteFileInf";
import { message, UploadFile } from "antd";
import { useForm } from "antd/es/form/Form";
import { useState, useCallback, useEffect } from "react";
export type FieldType = {
    name: string;
    avatar: string;
    email: string;
    gitee: string;
    github: string;
};
export const useConfigAdmin = () => {
    const [loading, setLoading] = useState(false);
    const [btnLoading, setBtnLoading] = useState(false);
    const [messageApi] = message.useMessage();
    const [fileList, setFileList] = useState<UploadFile[]>([]);
    const [form] = useForm();
    const [configInfo, setConfigInfo] = useState<ConfigType.Row<FieldType>>({
        id: "",
        title: "",
        configKey: "",
        config: "",
    });
    const getConfig = useCallback(async () => {
        setLoading(true);
        const res = await queryConfigBykey(ADMIN_INFO_KEY);
        setLoading(false);
        if (res.code === 200) {
            const obj = res.data || {};
            const config = obj.config ? JSON.parse(obj.config) : {};
            setConfigInfo({
                id: obj.id,
                title: obj.title,
                configKey: obj.configKey,
                config,
            });

            form.setFieldsValue({
                name: config.name || "",
                avatar: config.avatar || "",
                email: config.email || "",
                gitee: config.gitee || "",
                github: config.github || "",
            });
            if (config.avatar) {
                setFileList([
                    {
                        uid: "-1",
                        name: "avatar.png",
                        status: "done",
                        url: config.avatar,
                    },
                ]);
            }
        }
    }, [form]);

    /**
     * 保存配置
     * @param config
     */
    const saveConfigInf = async (values: FieldType) => {
        const { name, email, gitee, github } = values;
        const avatar = fileList[0] && fileList[0].url ? fileList[0].url : "";
        const config = {
            name,
            avatar,
            email,
            gitee,
            github,
        };
        const params = {
            id: configInfo?.id,
            configKey: ADMIN_INFO_KEY,
            config: JSON.stringify(config),
        };
        setBtnLoading(true);
        const res = await saveConfig(params);
        setBtnLoading(false);
        if (res.code === 200) {
            messageApi.success("保存成功");
            getConfig();
        }
    };
    const onFinish = async (values: FieldType) => {
        saveConfigInf(values);
    };

    const uploadFileInf = async (file: File | string | Blob) => {
        if (typeof file === "string") return;
        const formData = new FormData();
        formData.append("file", file);
        try {
            const res = await uploadFile(formData);
            const url = fileList[0] && fileList[0].url ? fileList[0].url : "";
            if (url) {
                deleteFileInf(url);
            }

            if (res.data) {
                setFileList([
                    {
                        uid: "-1",
                        name: "avatar.png",
                        status: "done",
                        url: res.data,
                    },
                ]);
            }
        } catch (error) {
            console.error(error);
        }
    };

    useEffect(() => {
        getConfig();
    }, [getConfig]);
    return {
        loading,
        btnLoading,
        form,
        onFinish,
        fileList,
        uploadFileInf,
        setFileList
    }
}